package cn.lixufeng.org.controller;

import cn.lixufeng.basic.exception.BusinessException;
import cn.lixufeng.basic.utils.AjaxResult;
import cn.lixufeng.basic.utils.PageList;
import cn.lixufeng.org.domain.Shop;
import cn.lixufeng.org.query.ShopQuery;
import cn.lixufeng.org.service.IShopService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/shop")
@Api(tags = "商家接口",description = "商家接口详细描述")
public class ShopController {

    @Autowired
    private IShopService shopService;

    /**
     * 增加或修改
     * @param shop
     * @return
     */
    @ApiOperation(value = "商家添加或修改",notes = "如果有id就是修改否则就是添加")
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Shop shop){
        try {
            if (shop.getId() == null) {
                shopService.add(shop);
            } else {
                shopService.update(shop);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("操作失败");
        }
    }

    /**
     * 按id删除
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            shopService.delete(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("操作失败");
        }
    }

    /**
     * 按id查询
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Shop findById(@PathVariable("id") Long id){
        return shopService.findById(id);
    }

    /**
     * 查询所有
     * @return
     */
    @GetMapping
    public List<Shop> findAll(){
        return shopService.findAll();
    }

    /**
     * 分页和高级查询
     * @param shopQuery
     * @return
     */
    @PostMapping
    public PageList<Shop> findPage(@RequestBody ShopQuery shopQuery){
        return shopService.findPage(shopQuery);
    }

    /**
     * 批量删除
     * @param ids 是需要删除的id的集合，前台传的是数组，后台可以用数组或者集合来接收
     * @return
     */
    @PatchMapping
    public AjaxResult patchDelete(@RequestBody List<Long> ids){
        try {
            shopService.batchDelete(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("操作失败");
        }
    }

    /**
     * 商家入驻
     * @param shop
     * @return
     */
    @PostMapping("/settlement")
    public AjaxResult settlement(@RequestBody Shop shop){
        try {
            shopService.settlement(shop);
            return AjaxResult.me();
        } catch (BusinessException e) {// 业务异常
            e.printStackTrace();
            return AjaxResult.me().setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("系统繁忙，请稍后再试");
        }
    }
}
